package com.huahua.api.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huahua.api.entity.pojo.ApiNovelBook;
import com.huahua.api.entity.pojo.ApiNovelBookContent;
import com.huahua.api.entity.pojo.ApiNovelType;
import com.huahua.api.entity.vo.novel.ApiBookContentVo;
import com.huahua.api.entity.vo.novel.ApiBookVo;
import com.huahua.api.mapper.ApiNovelBookMapper;
import com.huahua.api.processor.NovelPostProcessorExecutor;
import com.huahua.api.service.ApiNovelBookContentService;
import com.huahua.api.service.ApiNovelBookService;
import com.huahua.api.service.ApiNovelTypeService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author LoveHuahua
 * @date 2021年09月08日 9:55
 * @description believe in yourself
 */
@Service
public class ApiNovelBookServiceImpl extends ServiceImpl<ApiNovelBookMapper, ApiNovelBook> implements ApiNovelBookService {

    @Autowired
    ApiNovelTypeService typeService;

    @Autowired
    ApiNovelBookContentService contentService;

    @Autowired
    private NovelPostProcessorExecutor executor;

    @Override
    public ApiBookVo getBookDetail(int bookId) {
        ApiNovelBook book = lambdaQuery().eq(ApiNovelBook::getId, bookId).one();
        ApiBookVo bookVo = new ApiBookVo();
        //po => vo
        BeanUtils.copyProperties(book, bookVo);
        ApiNovelType type = typeService.lambdaQuery().eq(ApiNovelType::getId, bookVo.getTypeId()).one();

        //类型
        bookVo.setTypeText(type.getName());
        //章节数
        bookVo.setList(contentService.lambdaQuery().select(ApiNovelBookContent::getId, ApiNovelBookContent::getContentTitle).eq(ApiNovelBookContent::getBookId, bookId).list());
        return bookVo;
    }

    /**
     * @param bookId 书籍id
     * @param itemId 章节id
     * @return
     */
    @Override
    public ApiBookContentVo getBookItemDetail(int bookId, int itemId) {
        ApiNovelBookContent content = contentService.lambdaQuery().eq(ApiNovelBookContent::getBookId, bookId).eq(ApiNovelBookContent::getId, itemId).one();
        executor.execute(content);
        ApiBookContentVo contentVo = new ApiBookContentVo();
        //后置处理器
        contentVo.convert(content);
        Optional<ApiNovelBookContent> next = Optional.ofNullable(contentService.lambdaQuery().select(ApiNovelBookContent::getId, ApiNovelBookContent::getContentTitle).eq(ApiNovelBookContent::getBookId, bookId).gt(ApiNovelBookContent::getId, itemId).orderByAsc(ApiNovelBookContent::getId).last("limit 1").one());
        Optional<ApiNovelBookContent> pre = Optional.ofNullable(contentService.lambdaQuery().select(ApiNovelBookContent::getId, ApiNovelBookContent::getContentTitle).eq(ApiNovelBookContent::getBookId, bookId).lt(ApiNovelBookContent::getId, itemId).orderByDesc(ApiNovelBookContent::getId).last("limit 1").one());
        contentVo.setNextId(next.orElse(new ApiNovelBookContent()).getId());
        contentVo.setNextTitle(next.orElse(new ApiNovelBookContent()).getContentTitle());

        contentVo.setPreId(pre.orElse(new ApiNovelBookContent()).getId());
        contentVo.setPreTitle(pre.orElse(new ApiNovelBookContent()).getContentTitle());

        return contentVo;
    }

    /**
     * 搜索书籍
     *
     * @param searchKey 关键字
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<ApiNovelBook> searchBook(String searchKey, Integer page, Integer size) {
        return lambdaQuery().like(ApiNovelBook::getTitle,searchKey).or().like(ApiNovelBook::getAuthor,searchKey).page(new Page<>(page, size));
    }
}
